﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;

public partial class Controls_Users_ProfileBasicInformationEdit : System.Web.UI.UserControl
{
    private int m_Type;
    private User m_CurUser;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_CurUser = UserRules.Instance.GetCurrentUser();
        BintEvent();
        if (!IsPostBack)
        {
            InitDDLYear();
            InitDDLMonth();
            InitDDLDay(31);
            InitDDLFavoriteTechnical();
            InitUserBasicInfo();
        }
    }

    private void BintEvent()
    {
        ddl_month.SelectedIndexChanged += new EventHandler(ddl_month_SelectedIndexChanged);
        bt_Update.Click += new EventHandler(bt_Update_Click);
        txt_Email.Attributes.Add("onchange", "javascript:display();");
        ddl_FavoriteTechnical.SelectedIndexChanged += new EventHandler(ddl_FavoriteTechnical_SelectedIndexChanged);
    }


    private void InitUserBasicInfo()
    {
        lb_name.Text = m_CurUser.FullName;
        txt_Email.Text = m_CurUser.Email;
        if (m_CurUser.Gender == true)
        {
            ddl_Gender.SelectedValue = "1";
        }
        else
        {
            ddl_Gender.SelectedValue = "0";
        }
        DateTime birthday = m_CurUser.Birthday.Value;
        ddl_year.SelectedValue = birthday.Year.ToString();
        ddl_month.SelectedValue = birthday.Month.ToString();
        ddl_day.SelectedValue = birthday.Day.ToString();
        txt_Description.Text = m_CurUser.Description;
        InitTechnologyItems();
    }

    /// <summary>
    /// 当下拉列表框变化时初始化技术专长选项
    /// </summary>
    private void InitTechnologyItemsWhileDDLFTSelectChanged()
    {
        string[] tArray = m_CurUser.FavoriteTechnical.Split('-');
        if (tArray[0] != "")
        {
            foreach (string t in tArray)
            {
                for (int i = 0; i < cbl_Technology.Items.Count; i++)
                {
                    if (cbl_Technology.Items[i].Value == t)
                    {
                        cbl_Technology.Items[i].Selected = true;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 初始化技术专长的选项
    /// </summary>
    private void InitTechnologyItems()
    {
        string[] tArray = m_CurUser.FavoriteTechnical.Split('-');
        if (tArray[0] != "")
        {
            if (Convert.ToInt32(tArray[0]) <= 20)
            {
                ddl_FavoriteTechnical.SelectedValue = "Developer";
                cbl_Technology.Visible = true;
                InitCBLTechnology("Developer");
                cbl_Technology.RepeatColumns = 4;
            }
            else
            {
                ddl_FavoriteTechnical.SelectedValue = "Technician";
                cbl_Technology.Visible = true;
                InitCBLTechnology("Technician");
                cbl_Technology.RepeatColumns = 2;
            }
            foreach (string t in tArray)
            {
                for (int i = 0; i < cbl_Technology.Items.Count; i++)
                {
                    if (cbl_Technology.Items[i].Value == t)
                    {
                        cbl_Technology.Items[i].Selected = true;
                    }
                }
                up_UserBasicInfoForm.Update();
            }
        }
    }

    void bt_Update_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        lb_EmailExitingTip.Visible = false;
        DateTime birthday = Convert.ToDateTime(ddl_year.SelectedValue + "-" + ddl_month.SelectedValue + "-" + ddl_day.SelectedValue);
        if (UserRules.Instance.EmailUniqueExceptSelf(txt_Email.Text))
        {
            try
            {
                UserRules.Instance.UpdateUserBasicInfo(txt_Email.Text, Convert.ToBoolean(Convert.ToInt32(ddl_Gender.SelectedValue)), birthday, CheckTechnology(), txt_Description.Text);
                InitUserBasicInfo();
            }
            catch (Exception)
            {
            }
        }
        else
        {
            lb_EmailExitingTip.Visible = true;
        }
        this.up_UserBasicInfoForm.Update();
    }

    private string CheckTechnology()
    {
        string ft = "";
        for (int i = 0; i < cbl_Technology.Items.Count; i++)
        {
            if (this.cbl_Technology.Items[i].Selected)
            {
                if (ft == "")
                {
                    ft = cbl_Technology.Items[i].Value.ToString();
                }
                else
                {
                    ft += "-" + cbl_Technology.Items[i].Value.ToString();
                }
            }
        }
        return ft;
    }

    void ddl_FavoriteTechnical_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_FavoriteTechnical.SelectedValue == "null")
        {
            cbl_Technology.Visible = false;
            for (int i = 0; i < cbl_Technology.Items.Count; i++)
            {
                if (this.cbl_Technology.Items[i].Selected)
                {
                    cbl_Technology.Items[i].Selected = false;
                }
            }
            up_UserBasicInfoForm.Update();
        }
        else if (ddl_FavoriteTechnical.SelectedValue == "Developer")
        {
            cbl_Technology.Visible = true;
            InitCBLTechnology(ddl_FavoriteTechnical.SelectedValue);
            cbl_Technology.RepeatColumns = 4;
            InitTechnologyItemsWhileDDLFTSelectChanged();
            up_UserBasicInfoForm.Update();
        }
        else
        {
            cbl_Technology.Visible = true;
            InitCBLTechnology(ddl_FavoriteTechnical.SelectedValue);
            cbl_Technology.RepeatColumns = 2;
            InitTechnologyItemsWhileDDLFTSelectChanged();
            up_UserBasicInfoForm.Update();
        }
    }

    void ddl_month_SelectedIndexChanged(object sender, EventArgs e)
    {
        int currentmonth = Convert.ToInt32(ddl_month.SelectedValue.ToString());
        if (currentmonth == 1 || currentmonth == 3 || currentmonth == 5 || currentmonth == 7 || currentmonth == 8 || currentmonth == 10 || currentmonth == 12)
        {
            InitDDLDay(31);
        }
        else
        {
            InitDDLDay(30);
        }
    }

    private void InitCBLTechnology(string technologytype)
    {
        cbl_Technology.DataSource = DictionaryRules.Instance.GetTechnologies(technologytype);
        cbl_Technology.DataTextField = "TechnologyValue";
        cbl_Technology.DataValueField = "TechnologyId";
        cbl_Technology.DataBind();
    }

    private void InitDDLFavoriteTechnical()
    {
        ddl_FavoriteTechnical.Items.Add(new ListItem("--请选择--", "null"));
        ddl_FavoriteTechnical.Items.Add(new ListItem("开发人员", "Developer"));
        ddl_FavoriteTechnical.Items.Add(new ListItem("IT技术人员", "Technician"));
    }

    private void InitDDLYear()
    {
        ddl_year.Items.Clear();
        int i = 1900;
        while (i <= Convert.ToInt32(DateTime.Now.Year.ToString()))
        {
            ListItem yearitem = new ListItem();
            yearitem.Text = i.ToString();
            yearitem.Value = i.ToString();
            ddl_year.Items.Add(yearitem);
            i++;
        }
    }

    private void InitDDLMonth()
    {
        ddl_month.Items.Clear();
        int i;
        for (i = 1; i <= 12; i++)
        {
            ListItem monthitem = new ListItem();
            monthitem.Text = i.ToString();
            monthitem.Value = i.ToString();
            ddl_month.Items.Add(monthitem);
        }
    }

    private void InitDDLDay(int count)
    {
        ddl_day.Items.Clear();
        int i;
        for (i = 1; i <= count; i++)
        {
            ListItem dayitem = new ListItem();
            dayitem.Text = i.ToString();
            dayitem.Value = i.ToString();
            ddl_day.Items.Add(dayitem);
        }
    }

    #region Property
    public int ShowType  //是否是补全信息，还是修改个人基本资料
    {
        set
        {
            this.m_Type = value;
        }
    }
    #endregion
}
